//
//  EarningsInfoTableViewCell.swift
//  LeTu
//
//  Created by g1 on 2023/12/30.
//

import UIKit

class EarningsInfoTableViewCell: BaseTableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    static func getCellHeight() -> CGFloat {
        let height = (LanguageManager.shared.currentLanguage == .zh ? 0.0:16.0)
        return 24+18+8+42+(22+118)+18+height
    }
    func updateInfo(balance: Double) {
        priceLabel.text = "\(balance.formattedString())"
        earninsTitleLabel.text = "我的收益(元)".localized
        walletBtn.setTitle("去提现".localized, for: .normal)
        inviteInfoView.updateInfo()
        
        inviteInfoView.snp.updateConstraints { make in
            make.height.equalTo(118+(LanguageManager.shared.currentLanguage == .zh ? 0.0:16.0))
        }
    }
    
    // MARK: - UIButtonAction
    @objc func walletBtnAction() {
        send(event: EarningsVC.TransferEvent.toWithdraw)
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(earninsBackView)
        earninsBackView.addSubview(earninsTitleLabel)
        earninsBackView.addSubview(priceLabel)
        earninsBackView.addSubview(walletBtn)
        contentView.addSubview(inviteInfoView)
        
        earninsBackView.snp.makeConstraints { make in
            make.left.equalTo(24)
            make.right.equalTo(-24)
            make.top.equalTo(24)
        }
        earninsTitleLabel.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(18)
        }
        priceLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalTo(earninsTitleLabel.snp.bottom).offset(8)
            make.height.equalTo(42)
            make.bottom.equalToSuperview()
        }
        walletBtn.snp.makeConstraints { make in
            make.centerY.equalTo(priceLabel)
            make.right.equalToSuperview()
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 100:116)
            make.height.equalTo(36)
        }
        inviteInfoView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.top.equalTo(earninsBackView.snp.bottom).offset(22)
            make.height.equalTo(118+(LanguageManager.shared.currentLanguage == .zh ? 0.0:16.0))
        }
        
        walletBtn.addTarget(self, action: #selector(walletBtnAction), for: .touchUpInside)
    }
    private lazy var earninsBackView = UIView()
    private lazy var earninsTitleLabel: UILabel = {
        let earninsTitleLabel = UILabel()
        earninsTitleLabel.font = kSetPingFangRegular(12)
        earninsTitleLabel.textColor = .app_textGrayColor
        return earninsTitleLabel
    }()
    private lazy var priceLabel: UILabel = {
        let priceLabel = UILabel()
        priceLabel.textColor = .app_priceTextColor
        priceLabel.font = kSetPingFangBlod(35)
        return priceLabel
    }()
    private lazy var walletBtn: UIButton = {
        let walletBtn = UIButton()
        walletBtn.setImage(UIImage(named: "wallet_icon"), for: .normal)
        walletBtn.titleLabel?.font = kSetPingFangBlod(14)
        walletBtn.setTitleColor(.app_priceTextColor, for: .normal)
        walletBtn.layer.cornerRadius = 18
        walletBtn.layer.borderWidth = 1
        walletBtn.layer.borderColor = UIColor.hex(hexString: "#F3F3F3").cgColor
        walletBtn.layer.masksToBounds = true
        return walletBtn
    }()
    private lazy var inviteInfoView = EarningsInviteCodeView()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

// MARK: - 收益邀请信息
class EarningsInviteCodeView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.backView.layerCornerRadius(10)
        }
    }
    func updateInfo() {
        earninsLabel.text = "10元".localized
        inviteCodeLabel.text = "我的邀请码".localized+"：\(kUserManager.userModel.inviteCode)"
        largeBenefitsPromptLabel.text = "大额福利".localized
        invitePromptLabel.text = "邀请好友".localized
        wechatInviteBtn.setTitle("微信邀请".localized, for: .normal)
        invitePromptCodeLabel.text = "使用邀请码邀请朋友，可获得更多现金收益".localized
        
        wechatInviteBtn.snp.updateConstraints { make in
            make.right.equalTo(LanguageManager.shared.currentLanguage == .zh ? -16:-10)
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 94 : 138)
            make.centerY.equalToSuperview().offset(LanguageManager.shared.currentLanguage == .zh ? 0:-10)
        }
        largeBenefitsPromptLabel.snp.updateConstraints { make in
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 56:76)
        }
    }
    // MARK: - UIButtonAction
    @objc func copyBtnAction() {
        UIPasteboard.general.string = getPasteContent()
        Utils.showMessage(message: "复制成功".localized)
    }
    @objc func wechatInviteBtnAction() {
        guard kUserManager.isLogin else {
            kUserManager.toLogin()
            return
        }
        kUMengManager.umTextShare(text: getPasteContent(), platformType: .wechatSession)
    }
    private func getPasteContent() -> String {
        return """
                                        填我邀请码【\(kUserManager.userModel.inviteCode)】！
                                        🍅下载【乐途旅游讲解】
                                        🍅新用户当天必得超大现金红包！
                                        (好运连连，红包48小时内有效)
                                        快去商店搜索下载「乐途旅游讲解」吧
                                        """.localized
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(inviteEarninsView)
        inviteEarninsView.addSubview(largeBenefitsPromptLabel)
        inviteEarninsView.addSubview(invitePromptLabel)
        inviteEarninsView.addSubview(redPacketImgView)
        inviteEarninsView.addSubview(earninsLabel)
        backView.addSubview(lineView)
        backView.addSubview(inviteBackView)
        inviteBackView.addSubview(inviteContentView)
        inviteContentView.addSubview(inviteCodeLabel)
        inviteContentView.addSubview(copyBtn)
        inviteContentView.addSubview(invitePromptCodeLabel)
        backView.addSubview(wechatInviteBtn)

        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        inviteEarninsView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalToSuperview()
            make.height.equalTo(53)
        }
        largeBenefitsPromptLabel.snp.makeConstraints { make in
            make.centerY.left.equalToSuperview()
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 56:76)
            make.height.equalTo(18)
        }
        invitePromptLabel.snp.makeConstraints { make in
            make.left.equalTo(largeBenefitsPromptLabel.snp.right).offset(8)
            make.centerY.equalToSuperview()
        }
        redPacketImgView.snp.makeConstraints { make in
            make.left.equalTo(invitePromptLabel.snp.right).offset(8)
            make.width.height.equalTo(22)
            make.centerY.equalToSuperview()
        }
        earninsLabel.snp.makeConstraints { make in
            make.left.equalTo(redPacketImgView.snp.right).offset(6)
            make.centerY.equalToSuperview()
        }
        wechatInviteBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(LanguageManager.shared.currentLanguage == .zh ? -16:-10)
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 94 : 138)
            make.height.equalTo(30)
        }
        lineView.snp.makeConstraints { make in
            make.right.equalTo(wechatInviteBtn.snp.left).offset(-22)
            make.bottom.equalTo(inviteEarninsView.snp.bottom)
            make.height.equalTo(1)
            make.left.equalTo(inviteEarninsView)
        }
        inviteBackView.snp.makeConstraints { make in
            make.left.equalTo(inviteEarninsView)
            make.right.equalTo(wechatInviteBtn)
            make.top.equalTo(lineView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        inviteContentView.snp.makeConstraints { make in
            make.left.right.centerY.equalToSuperview()
        }
        inviteCodeLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(16)
        }
        copyBtn.snp.makeConstraints { make in
            make.left.equalTo(inviteCodeLabel.snp.right)
            make.width.equalTo(38)
            make.height.equalTo(inviteCodeLabel)
            make.centerY.equalTo(inviteCodeLabel)
        }
        invitePromptCodeLabel.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.height.equalTo(18)
            make.top.equalTo(inviteCodeLabel.snp.bottom).offset(2)
        }
        
        copyBtn.addTarget(self, action: #selector(copyBtnAction))
        wechatInviteBtn.addTarget(self, action: #selector(wechatInviteBtnAction), for: .touchUpInside)
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = UIColor.hex(hexString: "#FFFAEE")
        return backView
    }()
    private lazy var inviteEarninsView = UIView()
    private lazy var largeBenefitsPromptLabel: UILabel = {
        let largeBenefitsPromptLabel = UILabel()
        largeBenefitsPromptLabel.backgroundColor = .app_priceTextColor.withAlphaComponent(0.10)
        largeBenefitsPromptLabel.textColor = .app_priceTextColor
        largeBenefitsPromptLabel.textAlignment = .center
        largeBenefitsPromptLabel.layer.cornerRadius = 3
        largeBenefitsPromptLabel.layer.masksToBounds = true
        largeBenefitsPromptLabel.font = kSetPingFangBlod(12)
        return largeBenefitsPromptLabel
    }()
    private lazy var invitePromptLabel: UILabel = {
        let invitePromptLabel = UILabel()
        invitePromptLabel.font = kSetPingFangBlod(14)
        return invitePromptLabel
    }()
    private lazy var redPacketImgView: UIImageView = {
        let redPacketImgView = UIImageView()
        redPacketImgView.image = UIImage(named: "red_packet")
        redPacketImgView.contentMode = .scaleAspectFit
        return redPacketImgView
    }()
    private lazy var earninsLabel: UILabel = {
        let earninsLabel = UILabel()
        earninsLabel.textColor = .app_priceTextColor
        earninsLabel.font = kSetPingFangBlod(12)
        return earninsLabel
    }()
    private lazy var lineView = UIView()
    private lazy var wechatInviteBtn: UIButton = {
        let wechatInviteBtn = UIButton()
        wechatInviteBtn.setImage(UIImage(named: "invite_wechat"), for: .normal)
        wechatInviteBtn.titleLabel?.font = kSetPingFangBlod(12)
        wechatInviteBtn.setTitleColor(.app_black, for: .normal)
        wechatInviteBtn.backgroundColor = .white
        wechatInviteBtn.layer.cornerRadius = 15
        wechatInviteBtn.layer.masksToBounds = true
        wechatInviteBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -2, bottom: 0, right: 0)
        wechatInviteBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -2, bottom: 0, right: 0)
        return wechatInviteBtn
    }()
    // - 邀请相关信息
    private lazy var inviteBackView = UIView()
    private lazy var inviteContentView = UIView()
    private lazy var inviteCodeLabel: UILabel = {
        let inviteCodeLabel = UILabel()
        inviteCodeLabel.font = kSetPingFangRegular(14)
        return inviteCodeLabel
    }()
    private lazy var copyBtn: CustomClickableView = {
        let copyBtn = CustomClickableView()
        copyBtn.setImage(UIImage(named: "earnins_copy"))
        copyBtn.setImageEdgeInsets(UIEdgeInsets(top: 1, left: 12, bottom: 1, right: 12))
        return copyBtn
    }()
    private lazy var invitePromptCodeLabel: UILabel = {
        let invitePromptCodeLabel = UILabel()
        invitePromptCodeLabel.font = kSetPingFangRegular(12)
        invitePromptCodeLabel.textColor = .app_textGrayColor
        return invitePromptCodeLabel
    }()
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

